<?php

/**
 * Description of Application_Model_Base
 *
 * @author Jean Brito
 */
abstract class Application_Model_Base {

    protected $_table = '';
    public function getTable(){
        return $this->_table;
    }

    protected $_fields = Array();
    public function getFields(){
        return $this->_fields;
    }
    protected $_views = Array();
    public function getViews(){
        return $this->_views;
    }
    
    public function getView($view){
        if(!isset($this->_views[$view]))return null;
        return $this->_views[$view];
    }

    public function __construct() {
        $this->init();
    }

    public function init() {
        
    }

    public function getSqlPrimaryKEY($id) {
        $where = "";
        foreach ($this->getFields() as $field) {
            $fieldclass = 'Application_Model_Fields_' . $field['type'];
            $sqlSetup = $fieldclass::getCreateSql($field);
            if (isset($sqlSetup['sqlPrimaryKEY'])) {
                $where.="(" . $field['name'] . "=$id)";
            }
        }
        return $where;
    }
    
    public function getActions(&$model){
        return $model;
    }

    public function index($filter = null, $start = null, $limit = null, $sort = null) {
        $result=Application_Model_Db::index($this,$filter, $start, $limit, $sort);
        foreach($result['data'] as &$model){
            $this->getActions($model);
        }
        return $result;
    }

    public function get($id) {
        
    }

    public function post($data) {
        return Application_Model_Db::post($this,$data);
    }

    public function delete($id) {
        return Application_Model_Db::delete($this,$id);
    }

    public function put($id, $data) {
        return Application_Model_Db::put($this,$id,$data);
        
    }

}
